package com.itheima_01;

import java.util.Scanner;

/*遍历字符串：键盘录入一个字符串，使用程序实现在控制台遍历该字符串。*/
public class StringDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String s = sc.nextLine();
        /*System.out.println("您输入的字符串为：");
        System.out.print("{");
        for (int i = 0; i < s.length(); i++) {
            //返回字符串指定索引处的字符值
            char c = s.charAt(i);
            if (i < s.length()-1) {
                System.out.print(c + ", ");
            } else {
                System.out.println(c + "}");
            }
        }*/
        //将字符串拆分为字符数组
        char[] chars = s.toCharArray();
        System.out.println("您输入的字符串为：");
        System.out.print("{");
        for (int i = 0; i < chars.length; i++) {
            if (i < chars.length-1) {
                System.out.print(chars[i] + ", ");
            } else {
                System.out.println(chars[i] + "}");
            }
        }
    }
}